﻿using Lidgren.Network;
using Microsoft.Xna.Framework;
using System;

namespace BattleFury.Lib.Network.Packets
{
    /// <summary>
    /// The Move Packet represents a desired move from Client to Server.  
    /// 
    /// Only 1 move packet will be stored per client by the server, so any new packets will replace an 
    /// existing move packet
    /// </summary>
    [Serializable]
    public class MovePacket : IPacket
    {
        PacketCommand IPacket.Command { get { return PacketCommand.Move; } }
        NetDeliveryMethod IPacket.NetDeliveryMethod { get { return NetDeliveryMethod.UnreliableSequenced; } }
        long IPacket.ServerTime
        {
            get { return _servertime; }
        }

        private long _servertime;

        public long SourceObjectId { get; set; }
        public Vector2 Destination { get; set; }
        public float Velocity { get; set; }


        public MovePacket()
        { }

        public MovePacket(NetIncomingMessage msg)
        {
            SourceObjectId = msg.ReadInt64();
            Velocity = msg.ReadFloat();
            Destination = new Vector2(msg.ReadFloat(), msg.ReadFloat());
            _servertime = msg.ReadInt64();

        }
        public override string ToString()
        {
            return string.Format("Client ID:{0}   Destination:{1}  Velocity:{2} Duration:{3}", SourceObjectId, Destination, Velocity);
        }
        NetOutgoingMessage IPacket.ToOutgoingMessage(NetClient client)
        {
            NetOutgoingMessage msg = client.CreateMessage();
            msg.Write((int)((IPacket)this).Command);
            msg.Write(SourceObjectId);
            msg.Write(Velocity);
            msg.Write(Destination.X);
            msg.Write(Destination.Y);
            msg.Write(_servertime);
            return msg;
        }
    }

}